cmake_minimum_required(VERSION 3.10)
message("C Compiler: ${CMAKE_C_COMPILER}")
message("C++ Compiler: ${CMAKE_CXX_COMPILER}")
set(CMAKE_TOOLCHAIN_FILE "c:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    message(STATUS "Using GNU g++ compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message(STATUS "Using Clang compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    message(STATUS "Using Microsoft Visual C++ compiler")
else()
    message(STATUS "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

message(STATUS "CMAKE_SYSTEM_NAME:${CMAKE_SYSTEM_NAME}")
# if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    message(STATUS "Running on Windows")
    set(VCPKG_INSTALLED_DIR C:/vcpkg/installed/x64-windows)
    set(_VCPKG_INSTALLED_DIR C:/vcpkg/installed/x64-windows)
    message(STATUS "VCPKG_INSTALLED_DIR:${VCPKG_INSTALLED_DIR}")
# endif()

set(CMAKE_PREFIX_PATH ${VCPKG_INSTALLED_DIR}/share)

# 設置可執行文件的輸出目錄
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 設置動態庫的輸出目錄（如果有）
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 設置靜態庫的輸出目錄（如果有）
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 项目名称
project(event)
message(STATUS "CMAKE_SYSTEM_NAME:${CMAKE_SYSTEM_NAME}")

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    message(STATUS "VCPKG_INSTALLED_DIR:${VCPKG_INSTALLED_DIR}")

    set(OPENSSL_INCLUDE_DIR ${VCPKG_INSTALLED_DIR}/include)
    set(OPENSSL_ROOT_DIR ${VCPKG_INSTALLED_DIR})
    set(OPENSSL_CRYPTO_LIBRARY ${VCPKG_INSTALLED_DIR}/lib)
    # include_directories(${VCPKG_INSTALLED_DIR}/include)
    # link_directories(${VCPKG_INSTALLED_DIR}/lib)

    set(event_source src/iocp_server.cpp) # Windows 下使用 select/poll/Winsock 的源文件
    # add_definitions(-DBOOST_ASIO_DISABLE_BUFFER_DEBUGGING)

    # find_package(OpenSSL REQUIRED)
    # find_package(Boost REQUIRED COMPONENTS asio beast coroutine system thread)


    # include(c:/vcpkg/installed/x64-windows/share/openssl/OpenSSLConfig.cmake)
    # include(c:/vcpkg/installed/x64-windows/share/boost_beast/boost_beast-config.cmake)
    # include(c:/vcpkg/installed/x64-windows/share/boost_asio/boost_asio-config.cmake)
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(event_source src/event_epoll.c)
    # target_link_libraries(event ...) # 链接需要的库
elseif(CMAKE_SYSTEM_NAME MATCHES "MSYS")
    set(event_source src/iocp_server.cpp) # Windows 下使用 select/poll/Winsock 的源文件
else()
    message(FATAL_ERROR "Unsupported operating system${CMAKE_SYSTEM_NAME}")
endif()
    message(STATUS "compiler:${CMAKE_CXX_COMPILER}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    message(STATUS "Using GNU g++ compiler")
    # add_compile_definitions(-g)
    add_definitions(-g)
    add_definitions(-O0)
    add_definitions(-Wall)
endif()

find_package(Boost REQUIRED COMPONENTS system)

add_executable(iocp_server ${event_source}) # Windows 下使用 select/poll/Winsock 的源文件
# add_executable(reverse_proxy src/reverse_proxy.cpp)
add_executable(asio_tcp_server src/asio_tcp_server.cpp)
target_include_directories(asio_tcp_server PRIVATE ${VCPKG_INSTALLED_DIR}/include)
target_link_directories(asio_tcp_server PRIVATE ${VCPKG_INSTALLED_DIR}/lib)
target_link_libraries(asio_tcp_server Boost::system)


if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    # target_include_directories(event PRIVATE C:\\vcpkg\\installed\\x64-windows\\include)
    # target_link_directories(event PRIVATE c:\\vcpkg\\installed\\x64-windows\\lib)
    target_link_libraries(iocp_server ws2_32 kernel32 Mswsock) # libssl libcrypto) # 链接需要的库

    # target_include_directories(reverse_proxy PRIVATE C:\\vcpkg\\installed\\x64-windows\\include)
    # target_link_directories(reverse_proxy PRIVATE c:\\vcpkg\\installed\\x64-windows\\lib)
    # target_link_libraries(reverse_proxy ssl crypto) # 链接需要的库
    # target_link_libraries(reverse_proxy PRIVATE OpenSSL::Crypto OpenSSL::SSL Boost::asio Boost::beast)
    # target_link_libraries(reverse_proxy Boost::asio Boost::beast Boost::coroutine Boost::system Boost::thread OpenSSL::SSL OpenSSL::Crypto)

elseif(CMAKE_SYSTEM_NAME MATCHES "MSYS")
    target_include_directories(iocp_server PRIVATE /clang64/include)
    target_link_directories(iocp_server PRIVATE /clang64/lib)
    target_link_libraries(iocp_server ws2_32 kernel32 Mswsock ssl crypto) # 链接需要的库

    # target_compile_definitions(reverse_proxy PRIVATE __USE_W32_SOCKETS)

    # target_include_directories(reverse_proxy PRIVATE /clang64/include)
    # target_link_directories(reverse_proxy PRIVATE /clang64/lib)
    # target_link_libraries(reverse_proxy ssl crypto) # 链接需要的库
endif()

# 检查并设置默认构建类型
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
  message(STATUS "CMAKE_BUILD_TYPE not set, defaulting to Debug")
endif()

message("CMAKE_SOURCE_DIR:${CMAKE_SOURCE_DIR}")
message("CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
message("CMAKE_BINARY_DIR is ${CMAKE_BINARY_DIR}")
# add_executable(${PROJECT_NAME} src/event.c)

